﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DI;
using Core.Dtos;
using Core.Messages;
using StructureMap;
using Core.Queries;
using StructureMap.AutoMocking;
using System.Transactions;
using Tests.Helpers;
using StructureMap.Pipeline;
using Repositories;
using Front.Areas.Admin.Controllers.Api;

namespace Insurance.Test
{
    [TestClass]
    public class RoleApiControllerTest
    {
        private string cleanupId;
        private RoleApiController controller;        
        private ControllerHelper<RoleApiController> controllerHelper;

        [ClassInitialize()]
        void Initialize()
        {            
        }

        [TestInitialize()]
        public void InitializeTest()
        {
            Bootstrapper.Init(new SingletonLifecycle());
            CurrentContextHelper.Logon();
            controllerHelper = new ControllerHelper<RoleApiController>();
            controllerHelper.Initialize();
            controller = controllerHelper.GetController();
            cleanupId = null;
        }

        [TestCleanup()]
        public void CleanupTest()
        {
            if (cleanupId != null)
                controller.Delete(cleanupId);

            controllerHelper.Cleanup();
            Bootstrapper.Resolve<AppIdentityDbContext>().Dispose();          
        }

        private void SetCleanupId(string id)
        {
            cleanupId = id;
        }

        [TestMethod]
        public void RoleApiController_CanCreate()
        {
            // Arrange                        

            // Act            
            var response = controller.Post(GetSampleDto());
            SetCleanupId(response.Value);

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Create failed");            
        }
      
        [TestMethod]
        public void RoleApiController_CanRead()
        {
            // Arrange                       
           var id = controller.Post(GetSampleDto()).Value;
           SetCleanupId(id);

            // Act            
           var dto = controller.Get(id);

            // Assert
            Assert.IsNotNull(dto, "Get failed");        
        }
        
        [TestMethod]
        public void RoleApiController_CanEdit()
        {
            // Arrange                        
            var dto = GetSampleDto();
            var id = controller.Post(dto).Value;
            SetCleanupId(id);
            dto.Id = id;            

            // Act                        
            dto.Name = "Rolet900";
            Response response = controller.Put(dto.Id, dto);

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Edit failed");
        }
        
        [TestMethod]
        public void RoleApiController_CanDelete()
        {
            // Arrange
            var id = controller.Post(GetSampleDto()).Value;
            SetCleanupId(id);

            // Act
            Response response = controller.Delete(id);

            //Assert            
            Assert.IsNotNull(response);
            Assert.AreEqual(true, response.Success, "Delete failed");
        }
       
        [TestMethod]
        public void RoleApiController_CanFindBy()
        {
            // Arrange                        
            var id = controller.Post(GetSampleDto()).Value;
            SetCleanupId(id);
            var query = new RoleQuery() { Name = "Rolet90" };            

            // Act            
            var response = controller.FindBy(query);

            // Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(response.List.Count, 1, "FindBy Failed");           
        }

        public RoleDto GetSampleDto()
        {
            return new RoleDto() { Name = "Rolet90" };
        }
    }
}
